package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.investor.dto.loan.FundInfoRes;
import com.jrx.anytxn.investor.dto.loan.InvestorRes;
import com.jrx.anytxn.investor.dto.loan.LoanMainInvestorInfoRes;
import com.jrx.anytxn.investor.entity.AmLoanMainInvestorInfo;
import com.jrx.anytxn.investor.mapper.ext.ExtAmLoanMainInvestorInfoMapper;
import com.jrx.anytxn.investor.service.ILoanMainInvestorInfoService;
import com.jrx.anytxn.param.dto.investor.FundTableRes;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.entity.PrInvestorTable;
import com.jrx.anytxn.param.service.investor.IFundTableService;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 贷款信息service实现类
 *
 * @author 黄秋平
 * @create 2020/6/18
 */
@Service
public class LoanMainInvestorInfoServiceImpl implements ILoanMainInvestorInfoService {

    private static final Logger logger = LoggerFactory.getLogger(LoanMainInvestorInfoServiceImpl.class);

    @Resource
    private ExtAmLoanMainInvestorInfoMapper extAmLoanMainInvestorInfoMapper;

    @Resource
    private IFundTableService fundTableService;

    @Resource
    private IPrInvestorTableService prInvestorTableService;

    @Override
    public LoanMainInvestorInfoRes getInfoByInveLoanIdAndCusId(String tenantId, String customerId, String investorId) throws TxnException {
        List<AmLoanMainInvestorInfo> loanMainInvestorInfoList = extAmLoanMainInvestorInfoMapper.selectByCusIdAndInvestorId(tenantId,customerId,investorId);
        LoanMainInvestorInfoRes res=new LoanMainInvestorInfoRes();
        AmLoanMainInvestorInfo amLoanMainInvestorInfo=new AmLoanMainInvestorInfo();
        if (CollectionUtils.isNotEmpty(loanMainInvestorInfoList)) {
            amLoanMainInvestorInfo=loanMainInvestorInfoList.get(0);
        }
        //获取出资方信息
        PrInvestorTableRes prInvestorTableRes=prInvestorTableService.findByTenIdAndInvestorId(tenantId,investorId);
        res= BeanMapping.copy(amLoanMainInvestorInfo,LoanMainInvestorInfoRes.class);
        res.setChineseName(prInvestorTableRes.getChineseName());
        return res;
    }

    @Override
    public FundInfoRes getFundAndInvestorInfo(String tenantId, String loanId, String fundId, String customerId) throws TxnException {
        FundInfoRes fundInfoRes=new FundInfoRes();
        fundInfoRes.setFundId(fundId);
        //获取资金源信息
        FundTableRes fundTableRes=fundTableService.findByFundIdAndTenId(tenantId,fundId);
        BigDecimal loanAmount=BigDecimal.ZERO;
        fundInfoRes.setChineseName(fundTableRes.getChineseName());
        List<InvestorRes> investorResList=new ArrayList<>();
        List<AmLoanMainInvestorInfo> amLoanMainInvestorInfoList=extAmLoanMainInvestorInfoMapper.findInvestorLoan(loanId,customerId,tenantId);
        if(CollectionUtils.isNotEmpty(amLoanMainInvestorInfoList)){
            for (AmLoanMainInvestorInfo amLoanMainInvestorInfo:amLoanMainInvestorInfoList) {
                //查询出资方信息
                PrInvestorTable prInvestorTable=prInvestorTableService.findInvestorByInvestorId(amLoanMainInvestorInfo.getInvestorId(),tenantId);
                InvestorRes investorRes=new InvestorRes();
                investorRes.setInvestorId(amLoanMainInvestorInfo.getInvestorId());
                investorRes.setLoanAmount(amLoanMainInvestorInfo.getLoanAmount());
                investorRes.setChineseName(prInvestorTable.getChineseName());
                investorResList.add(investorRes);
                loanAmount=loanAmount.add(amLoanMainInvestorInfo.getLoanAmount());
            }
        }
        fundInfoRes.setLoanAmount(loanAmount);
        fundInfoRes.setInvestorResList(investorResList);
        return fundInfoRes;
    }
}
